<template>
  <PageWrapper>
    <BasicTable
      title="设备分类"
      class="page-search-table"
      @register="registerTable"
      :row-selection="{ onChange: doSelection }"
    >
      <template #tableTitle>
        <a-button type="primary" @click="openAdd">新增</a-button>
        <a-button type="primary" danger @click="openDelMulit">删除</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              tooltip: '编辑',
              onClick: openEdit.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              color: 'error',
              popConfirm: {
                title: `确认删除品牌商:${record.name}吗？`,
                confirm: openDelete.bind(null, record),
              },
            },
          ]"
        />
      </template>
    </BasicTable>
    <Dialog @register="registerModel" @success="submitSuccess" />
  </PageWrapper>
</template>
<script lang="ts">
  import { defineComponent, reactive } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { useModal } from '/@/components/Modal';
  import { tableConfig } from './tableData';
  import Dialog from './dialog.vue';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { deleqtClassApi } from '/@/api/sys/brand/brand';
  export default defineComponent({
    components: {
      PageWrapper,
      BasicTable,
      Dialog,
      TableAction,
    },
    emits: ['select'],
    setup(_) {
      const { createMessage } = useMessage();
      const [registerModel, { openModal: openModal }] = useModal();
      const [registerTable, { reload }] = useTable(tableConfig);
      function openAdd() {
        openModal(true, {
          isUpdate: false,
        });
      }
      function submitSuccess(e) {
        reload();
        createMessage.success(e.isUpdate ? '修改成功' : '添加成功');
      }
      function openEdit(rowdata: Recordable) {
        openModal(true, {
          rowdata,
          isUpdate: true,
        });
      }
      //删除操作
      async function openDelete(rowData) {
        let dellist: Number[] = [];
        if (Array.isArray(rowData)) {
          dellist = rowData;
        } else {
          dellist.push(rowData.id);
        }
        await deleqtClassApi({
          id_list: dellist,
        });
        createMessage.success('删除成功');
        reload();
      }
      //多选删除
      function openDelMulit() {
        if (delArray.length == 0) {
          createMessage.warn('请选择删除项');
          return;
        }
        openDelete(delArray);
      }
      //多选中
      let delArray: Number[] = reactive([]);
      function doSelection(selectedRowKeys: []) {
        delArray = [...selectedRowKeys];
      }
      return {
        registerModel,
        openModal,
        registerTable,
        submitSuccess,
        openAdd,
        openEdit,
        openDelete,
        openDelMulit,
        doSelection,
      };
    },
  });
</script>
